package com.situ.web.controller;

import com.situ.web.service.ITeacherService;
import com.situ.web.service.impl.TeacherServiceImpl;
import com.situ.web.util.PageInfo;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet("/teacher")
public class TeacherController extends HttpServlet {
     private ITeacherService teacherService = new TeacherServiceImpl();
     // private IStudetnService studetnService = new StudentServiceImpl();
    // 在上一层不去new具体的子类
//    private ITeacherService teacherService;
//
//    public void setTeacherService(ITeacherService teacherService) {
//        this.teacherService = teacherService;
//    }

    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("UTF-8");
        String method = req.getParameter("method");
        if (method == null || method.equals("")) {
            method = "selectByPage";
        }
        switch (method) {
            case "selectByPage":
                selectByPage(req, resp);
                break;

        }
    }

    private void selectByPage(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        System.out.println("TeacherController.selectByPage");
        // 1、封装前台过来的数据
        String pageNoStr = req.getParameter("pageNo");
        if (pageNoStr == null || pageNoStr.equals("")) {
            pageNoStr = "1";
        }
        String pageSizeStr = req.getParameter("pageSize");
        if (pageSizeStr == null || pageSizeStr.equals("")) {
            pageSizeStr = "5";
        }
        int pageNo = Integer.parseInt(pageNoStr);
        int pageSize = Integer.parseInt(pageSizeStr);

        // 2、调用Service代码完成功能
        PageInfo pageInfo = teacherService.selectByPage(pageNo, pageSize);

        // 3、转发到界面展示
        req.setAttribute("pageInfo",pageInfo);
        req.getRequestDispatcher("/teacher_list.jsp").forward(req, resp);
    }
}
